//
//  XZStationToStationController.m
//  生活小助手
//
//  Created by lixiangzhou on 14-8-8.
//  Copyright (c) 2014年 lxz. All rights reserved.
//

#import "XZSTSController.h"
#import "XZTrainTool.h"
#import "XZSTSParam.h"
#import "XZSTSResult.h"
#import "XZSTSResultResult.h"
#import "XZSTSTrainInfo.h"
#import "XZFunctionKey.h"
#import "MBProgressHUD+MJ.h"
#import "XZSTSCell.h"
#import "XZTrainController.h"
#import "XZTrainNumController.h"

#define kFieldWidth 80

@interface XZSTSController () <UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) UIView *topView;
@property (weak, nonatomic) UITextField *startStationField;
@property (weak, nonatomic) UITextField *endStationField;
@property (assign, nonatomic) BOOL netOK;
@property (strong, nonatomic) NSArray *data;
@property (weak, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIView *sectionHeaderView;
@end

@implementation XZSTSController

- (UIView *)sectionHeaderView
{
    if (!_sectionHeaderView) {
        
        UIView *headerView = [[UIView alloc] init];
        headerView.height = kTrainSectionHeaderHeight;
        headerView.width = self.view.width;
        headerView.backgroundColor = [UIColor yellowColor];
        
        /* ------------------列次名称------------------*/
        UILabel *trainOppLabel = [[UILabel alloc] init];
        trainOppLabel.text = @"列车";
        trainOppLabel.font = kTrainSectionFont;
        trainOppLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:trainOppLabel];
        
        /* ------------------出发站------------------*/
        UILabel *startStationLabel = [[UILabel alloc] init];
        startStationLabel.text = @"始发站";
        startStationLabel.font = kTrainSectionFont;
        startStationLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:startStationLabel];
        
        /* ------------------终点站------------------*/
        UILabel *endStationLabel = [[UILabel alloc] init];
        endStationLabel.text = @"终点站";
        endStationLabel.font = kTrainSectionFont;
        endStationLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:endStationLabel];
        
        /* ------------------到达时间------------------*/
        UILabel *arrivedTimeLabel = [[UILabel alloc] init];
        arrivedTimeLabel.text = @"到达时间";
        arrivedTimeLabel.font = kTrainSectionFont;
        arrivedTimeLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:arrivedTimeLabel];
        
        /* ------------------发车时间------------------*/
        UILabel *leaveTimeLabel = [[UILabel alloc] init];
        leaveTimeLabel.text = @"发车时间";
        leaveTimeLabel.font = kTrainSectionFont;
        leaveTimeLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:leaveTimeLabel];
        
        /* ------------------里程------------------*/
        UILabel *mileageLabel = [[UILabel alloc] init];
        mileageLabel.text = @"里程";
        mileageLabel.font = kTrainSectionFont;
        mileageLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:mileageLabel];
        
        /* ------------------设置位置------------------*/
        trainOppLabel.width = kTrainOppWidth;
        trainOppLabel.height = headerView.height;
        
        // 站点
        startStationLabel.x = CGRectGetMaxX(trainOppLabel.frame);
        startStationLabel.height = headerView.height * 0.5;
        startStationLabel.width = kStationWidth;
        
        endStationLabel.height = headerView.height * 0.5;
        endStationLabel.width = kStationWidth;
        endStationLabel.x = startStationLabel.x;
        endStationLabel.y = CGRectGetMaxY(startStationLabel.frame);
        
        // 时间
        arrivedTimeLabel.x = CGRectGetMaxX(startStationLabel.frame);
        arrivedTimeLabel.height = headerView.height * 0.5;
        arrivedTimeLabel.width = kTimeWidth;
        
        leaveTimeLabel.width = kTimeWidth;
        leaveTimeLabel.height = headerView.height * 0.5;
        leaveTimeLabel.x = arrivedTimeLabel.x;
        leaveTimeLabel.y = CGRectGetMaxY(arrivedTimeLabel.frame);
        
        // 里程
        mileageLabel.x = CGRectGetMaxX(arrivedTimeLabel.frame);
        mileageLabel.width = kMileageWidth;
        mileageLabel.height = headerView.height;
        
        _sectionHeaderView = headerView;
    }
    return _sectionHeaderView;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] init];
        tableView.sectionHeaderHeight = kTrainSectionHeaderHeight;
        tableView.rowHeight = kTrainRowHeight;
        tableView.width = self.view.width;
        tableView.height = self.view.height - CGRectGetMaxY(self.topView.frame);
        tableView.y = self.view.height - tableView.height;
        tableView.bounces = NO;
        tableView.delegate = self;
        tableView.dataSource = self;
        [self.view addSubview:tableView];
        _tableView = tableView;
    }
    return _tableView;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = kGlobalBackgroundColor;
    self.view.y = 93;
    self.view.height = kScreenHeight - self.view.y;
    
    [self addViews];
}


- (void)addViews
{
    /* ------------------顶部的查找------------------*/
    UIView *topView = [[UIView alloc] init];
    CGFloat topRowHeight = 30;
    topView.y = kMargin;
    [self.view addSubview:topView];
    self.topView = topView;
    
    /* ------------------始发站------------------*/
    UILabel *label = [[UILabel alloc] init];
    label.font = kTrainNormalFont;
    label.text = @"始发站：";
    CGSize size = [label.text sizeWithAttributes:@{NSFontAttributeName: label.font}];
    label.width = size.width;
    label.height = topRowHeight;
    [topView addSubview:label];
    
    UITextField *startStationField = [UITextField textFieldWithPlaceholder:@"如北京" leftMargin:5];
    startStationField.font = kTrainNormalFont;
    startStationField.x = CGRectGetMaxX(label.frame);
    startStationField.width = kFieldWidth;
    startStationField.height = label.height;
    self.startStationField = startStationField;
    [topView addSubview:startStationField];
    
    /* ------------------终点站------------------*/
    UILabel *label2 = [[UILabel alloc] init];
    label2.font = kTrainNormalFont;
    label2.text = @"终点站：";
    CGSize size2 = [label2.text sizeWithAttributes:@{NSFontAttributeName: label2.font}];
    label2.width = size2.width;
    label2.height = topRowHeight;
    label2.x = CGRectGetMaxX(startStationField.frame) + kMargin;
    [topView addSubview:label2];
    
    UITextField *endStationField = [UITextField textFieldWithPlaceholder:@"如江西" leftMargin:5];
    endStationField.font = kTrainNormalFont;
    endStationField.x = CGRectGetMaxX(label2.frame);
    endStationField.width = kFieldWidth;
    endStationField.height = label.height;
    self.endStationField = endStationField;
    [topView addSubview:endStationField];
    
    topView.width = CGRectGetMaxX(endStationField.frame) - CGRectGetMinX(label.frame);
    topView.centerX = self.view.centerX;
    
    
    UIButton *queryBtn = [[UIButton alloc] init];
    [queryBtn setTitle:@"查询" forState:UIControlStateNormal];
    [queryBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    queryBtn.backgroundColor = [UIColor orangeColor];
    queryBtn.height = topRowHeight;
    queryBtn.width = topView.width;
    queryBtn.y = CGRectGetMaxY(startStationField.frame) + kMargin;
    [queryBtn addTarget:self action:@selector(getTrains) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:queryBtn];
    
    topView.height = CGRectGetMaxY(queryBtn.frame);
}

- (void)getTrains
{
    NSString *start = self.startStationField.text;
    NSString *end = self.endStationField.text;

    XZSTSParam *param = [[XZSTSParam alloc] init];
    param.start = start;
    param.end = end;
    param.key = [XZFunctionKey trainKey];
    
    [MBProgressHUD showMessage:@"正在帮您查询"];
    [XZTrainTool stationToStationWithParameter:param success:^(XZSTSResult *responseObj) {
        self.netOK = YES;
        [self processResponse:responseObj];
        [MBProgressHUD hideHUD];
    } failure:^(NSError *error) {
        self.netOK = YES;
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:error.domain];
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (!self.netOK) {
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"请检查您的网络"];
        }
    });
    
    [self.view endEditing:YES];
}

- (void)processResponse:(XZSTSResult *)responseObj
{
    XZSTSResultResult *result = responseObj.result;
    self.data = result.data;
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource, UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XZSTSCell *cell = [XZSTSCell cellWithTableView:tableView];
    XZSTSTrainInfo *train = self.data[indexPath.row];
    cell.trainInfo = train;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.sectionHeaderView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kTrainRowHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    XZTrainController *trainController = (XZTrainController *)[self parentViewController];
    XZTrainNumController *trainNumController = trainController.controllerDict[@(XZTrainSubControllerTypeTrainNum)];
    XZSTSTrainInfo *train = self.data[indexPath.row];
    trainNumController.trainNo = train.trainOpp;
    trainController.segementControl.selectedSegmentIndex = XZTrainSubControllerTypeTrainNum;
    [trainController changeValue:trainController.segementControl];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
